package com.yidu.hbatsams.action.emp;

import com.yidu.hbatsams.domain.*;
import com.yidu.hbatsams.service.AtsBaseSportService;
import com.yidu.hbatsams.service.emp.AtsEmpAthleteService;
import com.yidu.hbatsams.service.emp.AtsEmpCoachAthleteService;
import com.yidu.hbatsams.service.emp.AtsEmpCoachService;
import com.yidu.hbatsams.utils.Message;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 运动员和教练关系表的控制层
 */
@Controller
@RequestMapping("/UserseControllerCoachAthlete")
public class AtsEmpCoachAthleteController {
    @Resource
    AtsEmpCoachAthleteService atsEmpCoachAthleteService;
    @Resource
    AtsEmpCoachService atsEmpCoachService;
    @Resource
    AtsEmpAthleteService atsEmpAthleteService;
    @Resource
    AtsBaseSportService atsBaseSportService;
    /**
     * 查询所有加模糊查询
     * @param pageNum
     * @param pageSize
     * @param coachName
     * @return
     */
    @ResponseBody
    @RequestMapping("selectAll")
    public Map<String,Object> findAll(@RequestParam("page") Integer pageNum, @RequestParam("limit") Integer pageSize, String coachName){
        System.out.println("教练运动员的控制层查询");
        Map<String,Object> map = new HashMap<>();
        Long count = atsEmpCoachAthleteService.count();
        List<AtsEmpCoachAthlete> list = atsEmpCoachAthleteService.findAll(pageNum,pageSize,coachName);
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", count); // 总行数
        map.put("data", list);
        return map;
    }

    /**
     * 逻辑删除
     * @param caId
     * @return
     */
    @ResponseBody
    @RequestMapping("deleteByPrimaryKey")
    public Message deleteById(Integer caId){
        atsEmpCoachAthleteService.deleteByPrimaryKey(caId);
        Message mes = new Message();
        mes.setNum(1);
        mes.setMsg("删除成功！");
        return mes;
    }

    /**
     * 根据ID查询 用于修改显示
     * @param caId
     * @return
     */
    @ResponseBody
    @RequestMapping("selectByPrimaryKey")
    public AtsEmpCoachAthlete findById(Integer caId){
        return atsEmpCoachAthleteService.selectByPrimaryKey(caId);
    }

    /**
     * 查询教练员名称
     * @return
     */
    @ResponseBody
    @RequestMapping("findByCoachName")
    public List<AtsEmpCoach> findByCoachName(){
        return atsEmpCoachService.findByName();
    }

    /**
     * 查询运动员名称
     * @return
     */
    @ResponseBody
    @RequestMapping("findByAthleteName")
    public List<AtsEmpAthlete> findByAthleteName(){
        return atsEmpAthleteService.findByAthleteId();
    }

    /**
     * 查询项目名称
     * @return
     */
    @ResponseBody
    @RequestMapping("findBaseSportName")
    public List<AtsBaseSport> findBySportName(){
        return atsBaseSportService.findAllSportName();
    }

   /* *//**
     * 新增加修改
     * @param record
     * @return
     */
    @ResponseBody
    @RequestMapping("addUpdate")
    public Message insertUpdate(AtsEmpCoachAthlete record ){
        //根据教练员名称查询教练员ID
        int coachName =  Integer.valueOf(record.getCoachName());
        System.out.println(record.getCoachName()+"===========");
        AtsEmpCoach coach = atsEmpCoachService.findById(coachName);
        //查询到的教练员名称设置到ATS参数中
        record.setCoachName(coach.getCoachName());
        record.setCoachId(coachName);
        //根据运动员名称查ID
        int athleteName = Integer.valueOf(record.getAthleteName());
        AtsEmpAthlete athlete = atsEmpAthleteService.findById(athleteName);
        record.setAthleteName(athlete.getAthleteName());
        record.setAthleteId(athleteName);
        Message mes = new Message();
        if(record.getCaId() != null && record.getCaId() > 0){
            System.out.println("修改");
            atsEmpCoachAthleteService.updateByPrimaryKey(record);
        }else if(record.getCaId() == null){
            System.out.println("新增");
            atsEmpCoachAthleteService.insert(record);
            mes.setNum(1);
        }
        mes.setMsg("操作成功");
        return mes;
    }

}
